package org.itboys.pay.demo;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.pay.alipay.AlipayDirectUtils;
import org.itboys.pay.alipay.AlipayMobileService;
import org.itboys.pay.alipay.AlipayWapResult;
import org.itboys.pay.constant.AlipayConfig;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-3-31 下午2:40:49 
 * 支付宝app支付异步回调操作demo 
 */
//@RestController
//@RequestMapping("/mobile/core/alipay")
public class CoreAliPayDemo extends BaseController {

	
	/**
	 * 支付宝支付完成后更改订单状态  异步    需要拆分订单的
	 * @param response
	 */
//	@RequestMapping("/notifyFinishOrder")
	public void notifyFinishOrder(HttpServletRequest request,
			HttpServletResponse response){
		try {
			AlipayWapResult wapResult = new AlipayWapResult();
			String key = "";//支付宝公钥
			try {
				wapResult = AlipayMobileService.notifyResult(request, key);
			} catch (Exception e1) {
				logger.error("解析阿里返回数据错误", e1);
				return;
			}
			logger.info("********notifyFinishOrder********"+wapResult.isVerify_result());
			String orderNo = wapResult.getOut_trade_no();// 外部订单ID
			logger.info("****orderNo***"+orderNo+"****TRADE_FINISHED*****"+wapResult.getTrade_status());
			if (wapResult.isVerify_result()) {
				//  TODO  支付宝支付成功后,比较订单金额和支付金额,更改订单状态等操作
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("/mobile/core/alipay/notifyFinishOrder error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	/**
	 * PC支付调用示例
	 * @param response
	 * @throws Exception
	 */
	public void demo(HttpServletResponse response) throws Exception{
		String orderNo = "";//订单编号
		String subject = "";//描述
		String buyerEmail = "";//卖家商户号
		BigDecimal totalFee = null;//支付金额
		String partner = "";//
		String returnUrl = "";
		String notifyUrl = "";
		String privateKey = "";
		//获得支付的跳转html代码
		String redirectAlipayUrl = AlipayDirectUtils.getRedirectAlipayUrl(orderNo, subject, buyerEmail, totalFee, partner, returnUrl, notifyUrl, privateKey);
		response.sendRedirect(redirectAlipayUrl);
	}
	/**
	 * 退款
	 * 支付宝退款需要我们打开一个新页面转跳到他的页面进行操作,所以这里返回一段html代码,浏览器上用这段代码去打开新页面
	 * @param response
	 */
	public void refund(HttpServletResponse response){
		String refundNotifyUrl = ""; //退款异步回调链接
		String outTradeNo = ""; //订单编号
		String totalFee = "";//退款金额
		String appId = "";
		String privateKey = "";
		String publicKey = "";
		try {
			String result = AlipayMobileService.refund(refundNotifyUrl, outTradeNo, totalFee, appId, privateKey, publicKey);
			AjaxUtils.renderHtml(response, result);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 扫码支付生成二维码示例
	 */
	/*public void test(){
		try {
			//调用接口生成二维码
			String qrcode = AlipayMobileService.createShopTwocode("2016101802218057", "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKyR1HKk5CznvJ9x\n" +
					"IOQ1eZAQD6oz4Vrda9yvojzQegz5owhdbXXOmGYdTpiVDmMa/bwFPJnsGmxzjznm\n" +
					"b5rpnXMKax8q2pcJ7Ge95t+n4V4Mm9uRMwb+i+BxX24vsI7Fu0Pbutwg6JaiMSwa\n" +
					"j66dcjPolHmfxZBPskbvds3rex+PAgMBAAECgYAyRblyPH8MZeYZPMesM7faNBOc\n" +
					"Lcj0w5Rske3dcY5cwUyxXyZIL4tOFDOVV7b9QBuy7oOdA58fAPs0juDsbColopEU\n" +
					"FATb4vDgiXdEg9e98eVbh6rrlr4bmn5y/oGv/fBGHNn2QCi6qgjKLkTywF1ag5hC\n" +
					"YkieeXxHs2bp3wTx4QJBAOKEyMRHnUihvDgZDJpLgK3s3yVPFAuUXBVpQqsqwY6+\n" +
					"715eAFefJwHWdAVMnqyTN8cl2JMfj6w5bYB2J02Jln0CQQDDB45GDwjh/7H7KPo3\n" +
					"9CGSV+rQvrx8u4WH9+uErKdMwgcvwHiIdK6A0wUCID1MbLQjwUEP7v1WrvOBJlbV\n" +
					"9E/7AkBzsqLhLjr12DZM6O54+MuXCn/HjKaOa47FGOiGiOYoRPn6V0URfCbuV10X\n" +
					"P9VkDCXXQEtoihxWOVbm4HKetgH5AkAOJ/0+vpaheOLdXTMUXqcVLUaBEmcyEpZ1\n" +
					"sRwqTDH0k1zcLXdSSE3mUeraJabtZjKJKdzkIC/JW/eW10aUTyCDAkEAjrvL0YMm\n" +
					"Wh04DYmdRk/lEdiJ1M/lQ9kovJXUVhcCwxw5/I9+EE77FyhUYfDM61mLLYyBEGch\n" +
					"JpgbC9EX8XaNCQ==", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8" +
					"fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf" +
					"8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssym" +
					"Zkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB", "12345678999", "0.1", "测试", null, "1", "60m", "C:\\Users\\Administrator\\Desktop");
			//撤销交易订单
			String cancelResult = AlipayMobileService.cancelTrade("2016101802218057", "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKyR1HKk5CznvJ9x\n" +
					"IOQ1eZAQD6oz4Vrda9yvojzQegz5owhdbXXOmGYdTpiVDmMa/bwFPJnsGmxzjznm\n" +
					"b5rpnXMKax8q2pcJ7Ge95t+n4V4Mm9uRMwb+i+BxX24vsI7Fu0Pbutwg6JaiMSwa\n" +
					"j66dcjPolHmfxZBPskbvds3rex+PAgMBAAECgYAyRblyPH8MZeYZPMesM7faNBOc\n" +
					"Lcj0w5Rske3dcY5cwUyxXyZIL4tOFDOVV7b9QBuy7oOdA58fAPs0juDsbColopEU\n" +
					"FATb4vDgiXdEg9e98eVbh6rrlr4bmn5y/oGv/fBGHNn2QCi6qgjKLkTywF1ag5hC\n" +
					"YkieeXxHs2bp3wTx4QJBAOKEyMRHnUihvDgZDJpLgK3s3yVPFAuUXBVpQqsqwY6+\n" +
					"715eAFefJwHWdAVMnqyTN8cl2JMfj6w5bYB2J02Jln0CQQDDB45GDwjh/7H7KPo3\n" +
					"9CGSV+rQvrx8u4WH9+uErKdMwgcvwHiIdK6A0wUCID1MbLQjwUEP7v1WrvOBJlbV\n" +
					"9E/7AkBzsqLhLjr12DZM6O54+MuXCn/HjKaOa47FGOiGiOYoRPn6V0URfCbuV10X\n" +
					"P9VkDCXXQEtoihxWOVbm4HKetgH5AkAOJ/0+vpaheOLdXTMUXqcVLUaBEmcyEpZ1\n" +
					"sRwqTDH0k1zcLXdSSE3mUeraJabtZjKJKdzkIC/JW/eW10aUTyCDAkEAjrvL0YMm\n" +
					"Wh04DYmdRk/lEdiJ1M/lQ9kovJXUVhcCwxw5/I9+EE77FyhUYfDM61mLLYyBEGch\n" +
					"JpgbC9EX8XaNCQ==", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q" +
					"8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJB" +
					"VHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmds" +
					"symZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB", "12345678999");
			System.out.println(cancelResult);
			//查询交易订单
			Integer result = AlipayMobileService.queryOrder("2016101802218057", "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKyR1HKk5CznvJ9x\n" +
					"IOQ1eZAQD6oz4Vrda9yvojzQegz5owhdbXXOmGYdTpiVDmMa/bwFPJnsGmxzjznm\n" +
					"b5rpnXMKax8q2pcJ7Ge95t+n4V4Mm9uRMwb+i+BxX24vsI7Fu0Pbutwg6JaiMSwa\n" +
					"j66dcjPolHmfxZBPskbvds3rex+PAgMBAAECgYAyRblyPH8MZeYZPMesM7faNBOc\n" +
					"Lcj0w5Rske3dcY5cwUyxXyZIL4tOFDOVV7b9QBuy7oOdA58fAPs0juDsbColopEU\n" +
					"FATb4vDgiXdEg9e98eVbh6rrlr4bmn5y/oGv/fBGHNn2QCi6qgjKLkTywF1ag5hC\n" +
					"YkieeXxHs2bp3wTx4QJBAOKEyMRHnUihvDgZDJpLgK3s3yVPFAuUXBVpQqsqwY6+\n" +
					"715eAFefJwHWdAVMnqyTN8cl2JMfj6w5bYB2J02Jln0CQQDDB45GDwjh/7H7KPo3\n" +
					"9CGSV+rQvrx8u4WH9+uErKdMwgcvwHiIdK6A0wUCID1MbLQjwUEP7v1WrvOBJlbV\n" +
					"9E/7AkBzsqLhLjr12DZM6O54+MuXCn/HjKaOa47FGOiGiOYoRPn6V0URfCbuV10X\n" +
					"P9VkDCXXQEtoihxWOVbm4HKetgH5AkAOJ/0+vpaheOLdXTMUXqcVLUaBEmcyEpZ1\n" +
					"sRwqTDH0k1zcLXdSSE3mUeraJabtZjKJKdzkIC/JW/eW10aUTyCDAkEAjrvL0YMm\n" +
					"Wh04DYmdRk/lEdiJ1M/lQ9kovJXUVhcCwxw5/I9+EE77FyhUYfDM61mLLYyBEGch\n" +
					"JpgbC9EX8XaNCQ==", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q" +
					"8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJB" +
					"VHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmds" +
					"symZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB", "12345678999");
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
}
